local jieming = fk.CreateSkill{
  name = "lingling__jieming",
}

Fk:loadTranslationTable{
  ["lingling__jieming"] = "节命",
  [":lingling__jieming"] = "当你受到1点伤害后或当你死亡时，你可以令一名角色摸X张牌，其将手牌弃置至X张。（X为其体力上限且至多为5）",

  ["#lingling__jieming-choose"] = "节命：你可以令一名角色摸X张牌并将手牌弃至X张（X为其体力上限且至多为5）",
}

local spec = {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      min_num = 1,
      max_num = 1,
      prompt = "#lingling__jieming-choose",
      skill_name = jieming.name,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event:getCostData(self).tos[1]
    to:drawCards(math.min(to.maxHp, 5), jieming.name)
    if to.dead then return false end
    local x = to:getHandcardNum() - math.min(to.maxHp, 5)
    if x > 0 then
      player.room:askToDiscard(to, {
        min_num = x,
        max_num = x,
        include_equip = false,
        skill_name = jieming.name,
        cancelable = false,
      })
    end
  end,
}

jieming:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jieming.name) and player == target
  end,
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

jieming:addEffect(fk.Death, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

return jieming